﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Crude.Common;

namespace Crude.VisualStudioAddin.Forms
{
    public partial class SelectVersionForm : Form
    {
        public class VersionViewModel
        {
            public string Description { get; set; }
            public string Version { get; set; }

            public override string ToString()
            {
                return string.Format("{0}\t{1}", Version, Description);
            }
        }

        IList<VersionViewModel> m_versions;

        public SelectVersionForm(IList<ProjectObjectModel> versions, string currentVersion)
        {
            InitializeComponent();
            m_versions = (from versionPom in versions
                              select new VersionViewModel 
                              {
                                  Description = versionPom.Description,
                                  Version = versionPom.Version
                              }).ToList();
                                  

            m_versions = m_versions.OrderBy(v=>v.Version).ToList();
            m_versions.Add(new VersionViewModel
                {
                    Version = "*",
                    Description = m_versions.Last().Description
                });
            m_versions = m_versions.Reverse().ToList();

            m_versionListBox.DataSource = m_versions;
            CurrentVersion = currentVersion;
            m_selectedVersionTextBox.Text = currentVersion;            
        }

        public string CurrentVersion { get; set; }

        private void m_versionListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void m_versionListBox_DoubleClick(object sender, EventArgs e)
        {
            m_selectedVersionTextBox.Text = ((VersionViewModel)m_versionListBox.SelectedItem).Version;
        }

        private void m_okButton_Click(object sender, EventArgs e)
        {
            CurrentVersion = m_selectedVersionTextBox.Text;
            DialogResult = System.Windows.Forms.DialogResult.OK;

            Close();
        }
    }
}
